/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.google.common.base.Charsets;
/*     */ import com.ibm.icu.text.CharsetDetector;
/*     */ import com.ibm.icu.text.CharsetMatch;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import com.zimbra.cs.service.mail.ImportContacts;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.servlet.util.JettyUtil;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintWriter;
/*     */ import java.io.PushbackInputStream;
/*     */ import java.nio.charset.Charset;
/*     */ import java.util.EnumSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CsvFormatter
/*     */   extends Formatter
/*     */ {
/*     */   private static final int READ_AHEAD_BUFFER_SIZE = 8192;
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/*  56 */     return FormatterFactory.FormatType.CSV;
/*     */   }
/*     */   
/*     */   public String[] getDefaultMimeTypes()
/*     */   {
/*  61 */     return new String[] { "text/csv", "text/comma-separated-values", "text/plain" };
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> getDefaultSearchTypes()
/*     */   {
/*  66 */     return EnumSet.of(MailItem.Type.CONTACT);
/*     */   }
/*     */   
/*     */   public void formatCallback(UserServletContext context)
/*     */     throws IOException, ServiceException
/*     */   {
/*  72 */     disableJettyTimeout(context);
/*     */     
/*  74 */     Iterator<? extends MailItem> iterator = null;
/*  75 */     StringBuilder sb = new StringBuilder();
/*     */     try {
/*  77 */       iterator = getMailItems(context, -1L, -1L, 2147483647L);
/*  78 */       String format = context.req.getParameter("csvfmt");
/*  79 */       String locale = context.req.getParameter("csvlocale");
/*  80 */       String separator = context.req.getParameter("csvsep");
/*  81 */       Character sepChar = null;
/*  82 */       if ((separator != null) && (separator.length() > 0))
/*  83 */         sepChar = Character.valueOf(separator.charAt(0));
/*  84 */       if (locale == null) {
/*  85 */         locale = context.getLocale().toString();
/*     */       }
/*     */       
/*  88 */       ContactCSV contactCSV = new ContactCSV(context.targetMailbox, context.opContext);
/*  89 */       contactCSV.toCSV(format, locale, sepChar, iterator, sb);
/*     */     } catch (ContactCSV.ParseException e) {
/*  91 */       throw MailServiceException.UNABLE_TO_IMPORT_CONTACTS("could not generate CSV", e);
/*     */     } finally {
/*  93 */       if ((iterator instanceof Formatter.QueryResultIterator)) {
/*  94 */         ((Formatter.QueryResultIterator)iterator).finished();
/*     */       }
/*     */     }
/*     */     
/*  98 */     String filename = context.itemPath;
/*  99 */     if ((filename == null) || (filename.length() == 0))
/* 100 */       filename = "contacts";
/* 101 */     if (!filename.toLowerCase().endsWith(".csv")) {
/* 102 */       filename = filename + ".csv";
/*     */     }
/* 104 */     String cd = HttpUtil.createContentDisposition(context.req, "attachment", filename);
/* 105 */     context.resp.addHeader("Content-Disposition", cd);
/* 106 */     context.resp.setCharacterEncoding(context.getCharset().name());
/* 107 */     context.resp.setContentType("text/csv");
/* 108 */     context.resp.getWriter().print(sb.toString());
/*     */   }
/*     */   
/*     */   public boolean supportsSave()
/*     */   {
/* 113 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename)
/*     */     throws UserServletException, ServiceException, IOException
/*     */   {
/* 122 */     disableJettyTimeout(context);
/*     */     
/* 124 */     PushbackInputStream pis = new PushbackInputStream(context.getRequestInputStream(), 8192);
/* 125 */     byte[] buf = new byte[' '];
/* 126 */     int bytesRead = pis.read(buf, 0, 8192);
/* 127 */     CharsetDetector detector = new CharsetDetector();
/* 128 */     detector.setText(buf);
/* 129 */     CharsetMatch match = detector.detect();
/* 130 */     String guess = match.getName();
/*     */     Charset charset;
/* 132 */     if (guess != null) {
/*     */       try {
/* 134 */         charset = Charset.forName(guess);
/*     */       } catch (IllegalArgumentException e) {
/* 136 */         Charset charset = Charsets.UTF_8;
/*     */       }
/*     */     } else {
/* 139 */       charset = Charsets.UTF_8;
/*     */     }
/* 141 */     if (bytesRead > 0) {
/* 142 */       pis.unread(buf, 0, bytesRead);
/*     */     }
/* 144 */     InputStreamReader isr = new InputStreamReader(pis, charset);
/* 145 */     BufferedReader reader = new BufferedReader(isr);
/*     */     try
/*     */     {
/* 148 */       String format = (String)context.params.get("csvfmt");
/* 149 */       String locale = context.req.getParameter("csvlocale");
/* 150 */       if (locale == null) {
/* 151 */         locale = context.getLocale().toString();
/*     */       }
/* 153 */       List<Map<String, String>> contacts = ContactCSV.getContacts(reader, format, locale);
/* 154 */       ItemId iidFolder = new ItemId(folder);
/*     */       
/* 156 */       ImportContacts.ImportCsvContacts(context.opContext, context.targetMailbox, iidFolder, contacts);
/*     */     } catch (ContactCSV.ParseException e) {
/* 158 */       ZimbraLog.misc.debug("ContactCSV - ParseException thrown", e);
/* 159 */       throw new UserServletException(400, "Could not parse csv file - Reason : " + e.getMessage());
/*     */     }
/*     */     finally {
/* 162 */       reader.close();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void disableJettyTimeout(UserServletContext context)
/*     */   {
/* 184 */     if (LC.zimbra_csv_formatter_disable_timeout.booleanValue()) {
/* 185 */       JettyUtil.setIdleTimeout(0L, context.req);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/CsvFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */